import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:winkai/entity/subscribe_from.dart';
import 'package:winkai/helper/iap/iap_helper.dart';
import 'package:winkai/helper/user_helper.dart';
import 'package:winkai/res/app_colors.dart';
import 'package:winkai/res/dimens.dart';
import 'package:winkai/ui/user/controller/setting_controller.dart';
import 'package:winkai/ui/user/dialog/edit_name_dialog.dart';
import 'package:winkai/ui/user/widget/setting_vip_info_widget.dart';
import 'package:winkai/utils/image_utils.dart';
import 'package:winkai/utils/page_jump_utils.dart';
import 'package:winkai/widget/custom_style_bg.dart';
import 'package:winkai/widget/normal_action_widget.dart';
import 'package:winkai/widget/visibility_change_widget.dart';

class SettingPage extends StatefulWidget {
  const SettingPage({super.key});

  @override
  State<SettingPage> createState() => _SettingPageState();
}

class _SettingPageState extends State<SettingPage> {
  final controller = Get.put(SettingController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: VisibilityChangeWidget(
        visibilityKey: const ValueKey('SettingPage'),
        onVisibility: () {
          controller.refreshData();
        },
        child: AnnotatedRegion<SystemUiOverlayStyle>(
          value: SystemUiOverlayStyle.dark,
          child: Column(
            children: [
              SafeArea(
                left: false,
                right: false,
                bottom: false,
                child: Container(
                  padding: EdgeInsets.only(
                    left: Dimens.mainHorizontalSpacing,
                    top: 6.w,
                    right: 6.w,
                  ),
                  alignment: Alignment.centerLeft,
                  child: GestureDetector(
                    onTap: () {
                      Get.back();
                    },
                    child: ImageUtils.getImage('ic_ai_back', 36.w, 36.w),
                  ),
                ),
              ),
              Expanded(
                child: Obx(() {
                  controller.refreshStatus.value;
                  IAPHelper().subscribeStatus.value;
                  var isSubscribeVip = IAPHelper().isVip;
                  return CustomScrollView(
                    physics: const BouncingScrollPhysics(),
                    slivers: [
                      SliverPadding(
                        padding: EdgeInsets.symmetric(
                            horizontal: Dimens.mainHorizontalSpacing),
                        sliver: SliverToBoxAdapter(
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            mainAxisSize: MainAxisSize.min,
                            children: [
                              if (!isSubscribeVip)
                                GestureDetector(
                                  onTap: () {
                                    PageJumpUtils.toVipPage(
                                        SubscribeFrom.meVip);
                                  },
                                  child: const SettingVipInfoWidget(),
                                ),
                              SizedBox(height: 20.w),
                              CustomStyleBg(
                                borderRadius: 16,
                                color: Colors.white,
                                child: NormalActionWidget(
                                  actionName: 'setting_nickname'.tr,
                                  rightTips: UserHelper().user.nickname,
                                  onTap: () {
                                    showNameDialog(
                                      context,
                                      nickname: UserHelper().user.nickname,
                                      onComplete: () {
                                        controller.refreshData();
                                      },
                                    );
                                  },
                                ),
                              ),
                              SizedBox(height: 16.w),
                              _buildTitle('setting_support'.tr),
                              SizedBox(height: 8.w),
                              CustomStyleBg(
                                borderRadius: 16,
                                color: Colors.white,
                                child: NormalActionWidget(
                                  actionName: 'setting_feedback'.tr,
                                  onTap: () {
                                    PageJumpUtils.toFeedbackPage();
                                  },
                                ),
                              ),
                              SizedBox(height: 16.w),
                              _buildTitle('setting_legal'.tr),
                              SizedBox(height: 8.w),
                              Column(
                                mainAxisSize: MainAxisSize.min,
                                children: [
                                  CustomStyleBg(
                                    borderRadius: 16,
                                    color: Colors.white,
                                    child: Column(
                                      mainAxisSize: MainAxisSize.min,
                                      children: [
                                        NormalActionWidget(
                                          actionName: 'setting_privacy_policy'.tr,
                                          onTap: () {
                                            PageJumpUtils.toPrivacyPolicyPage();
                                          },
                                        ),
                                        Divider(
                                          height: 0.5.w,
                                          thickness: 0.5.w,
                                          indent: Dimens.mainHorizontalSpacing,
                                          endIndent: Dimens.mainHorizontalSpacing,
                                          color: AppColors.color_ed,
                                        ),
                                        NormalActionWidget(
                                          actionName: 'setting_terms_of_use'.tr,
                                          onTap: () {
                                            PageJumpUtils.toTermOfUsePage();
                                          },
                                          onLongPress: () {
                                            // if (!kReleaseMode) {
                                            //   NavigatorUtils.pushRoute(
                                            //       const SqliteViewerRoute());
                                            // }
                                          },
                                        ),
                                      ],
                                    ),
                                  ),
                                ],
                              )
                            ],
                          ),
                        ),
                      )
                    ],
                  );
                }),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildTitle(String title) {
    return SizedBox(
      width: double.infinity,
      child: Text(
        title,
        style: TextStyle(
          fontSize: 16.sp,
          color: AppColors.color_4d,
          fontWeight: FontWeight.w600,
        ),
      ),
    );
  }
}
